package com.sinodata.bsm.cicp.collector.mysql;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.DBConnectionUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: ylchkj</p>
 * @author not attributable
 * @version 1.0
 *
 * MySQL Database 采集
 */
public class Database extends Collector {
    private Logger logger = Logger.getLogger(Database.class);

    /**
     *
     */
    public Database() {
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String dbtype = getParamMap().get("DBType");
        String dbname = getParamMap().get("DBName");
        String name = getParamMap().get("DataBaseName");

        String catalog_name = "";
        String default_character_set_name = "";
        String default_collation_name = "";
        String sql_path = "";

        int version = MySQLUtil.currentVersion(dbtype, ip, dbname);
        if (version < 5) {
            throw new CollectorException("MySQL " + version + " is lower and has no system table!");
        }

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = DBConnectionUtil.getInstance().getConnection(dbtype, ip, dbname);
            stmt = conn.createStatement();

            String sql = "select * from information_schema.schemata where schema_name = '" + name + "'";
            rs = stmt.executeQuery(sql);

            while (rs.next()) {
                catalog_name = rs.getString("CATALOG_NAME");
                catalog_name = catalog_name == null ? "" : catalog_name.trim();
                default_character_set_name = rs.getString("DEFAULT_CHARACTER_SET_NAME");
                default_character_set_name = default_character_set_name == null ? "" : default_character_set_name.trim();
                default_collation_name = rs.getString("DEFAULT_COLLATION_NAME");
                default_collation_name = default_collation_name == null ? "" : default_collation_name.trim();
                sql_path = rs.getString("SQL_PATH");
                sql_path = sql_path == null ? "" : sql_path.trim();
            }
        } catch (Exception ex) {
            logger.error("Collect mysql(" + ip + ") information error", ex);
            throw new CollectorException("Collect mysql(" + ip + ") information error", ex);
        } finally {
            if (rs != null)
                try {
                    rs.close();
                } catch (SQLException e) {
                }
            if (stmt != null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            DBConnectionUtil.getInstance().returnConnection(conn);
        }

        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        String[] propMetas = getPropMeta();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        propValues[0] = ResPropertyValue.createPropValue(catalog_name, propMetas[0]);
        propValues[1] = ResPropertyValue.createPropValue(default_character_set_name, propMetas[1]);
        propValues[2] = ResPropertyValue.createPropValue(default_collation_name, propMetas[2]);
        propValues[3] = ResPropertyValue.createPropValue(sql_path, propMetas[3]);
        propertyValueX.setValues(propValues);
        values.add(propertyValueX);
        return values;

    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "DBType", "DBName" };
    }

    @Override
    public String[] getPropMeta() {
        return new String[] { "CATALOG_NAME", "DEFAULT_CHARACTER_SET_NAME", "DEFAULT_COLLATION_NAME", "SQL_PATH" };
    }
}
